package com.leyou.item.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public PageResult<Brand> findBrandByPageAndSort(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
        // 开始分页
        PageHelper.startPage(page, rows);
        //过滤
        Example example = new Example(Brand.class);
        if (StringUtils.isNotBlank(key)){
            example.createCriteria().andLike("name","%" +key+"%").orEqualTo("letter",key);
        }
        if(StringUtils.isNotBlank(sortBy)){
            //排序
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }
        //查询
        Page<Brand> pageInfo =  (Page<Brand>)brandMapper.selectByExample(example);
        return  new PageResult<>(pageInfo.getTotal(),pageInfo);

    }
    @Transactional
    @Override
    public void savebarnd(Brand brand, List<Long> cids) {
        // 新增品牌信息
        this.brandMapper.insert(brand);
        for (Long cid : cids) {
            this.brandMapper.insertCategoryBrand(cid,brand.getId());
        }

    }

    @Override
    public int updatebarnd(Brand brand, List<Long> cids) {
        return 0;
    }

    @Override
    public List<Brand> findBrandByCid(Long cid) {
        return  this.brandMapper.queryByCategoryId(cid);
    }

    @Override
    public List<Brand> queryBrandByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return  this.brandMapper.selectByIdList(ids);
    }

    @Override
    public Brand queryBrandByBid(Long bid) {
        if (bid==null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return  this.brandMapper.queryBrandByBid(bid);
    }

    @Override
    public String queryBrandNameByBid(Long bid) {
        if (bid==null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return this.brandMapper.queryBrandNameByBid(bid);

    }
    @Transactional
    @Override
    public int deleteBrand(Long bid) {
        if (bid==null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        int i = this.brandMapper.deleteByPrimaryKey(bid);
        brandMapper.deleteBrandAndCategoryByBid(bid);
        return i;
    }

    @Override
    public int updateBrand(Brand brand) {
        return  this.brandMapper.updateByPrimaryKeySelective(brand);
    }

}
